<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class MainMenu {
	var $title;
	var $iconCls;
	var $items;
}

class Menu {
	var $id;
	var $xtype;
	var $title;
}

class MenuResult {
	var $results;
}
class  KatilimcisController extends AppController {
	public $helpers = array('Html', 'Form');
	public $components = array('Paginator');
	public $paginate = array(
			'limit' => 5,
			'order' => array(
					'Katilimci.ad' => 'asc'
			)
	);
	
	public function index() {
		$this->autoRender = false;
		$this->layout = 'ajax';
		
		
		echo "asdas";
	}
	
	public function listKatilimci () {
		$this->layout = null ;
		
		$projeId = $this->request->query('projeId');
		
		//debug($projeId, true);
		
		$conditions = array();
		$conditions['Katilimci.ad LIKE'] = $this->request->query('ad') . '%';
		$conditions['Katilimci.soyad LIKE'] = $this->request->query('soyad') . '%';
		if ($projeId) {
			$conditions['Katilimci.proje_id = '] = $projeId;			
		}
		
		$this->Paginator->settings = $this->paginate;
		$this->Paginator->settings = array(
				'conditions' => $conditions/*array('Katilimci.ad LIKE' => $this->request->query('ad') . '%')*/,
				'limit' => $this->request->query('limit'),
				'page' => $this->request->query('page')
		);
		
		
		
		if ($this->request->query('sort')) {
			$sortArr = json_decode($this->request->query('sort'), true);
			if (count($sortArr) == 1) {
				$property =  $sortArr[0]['property'];
				$direction =  $sortArr[0]['direction'];
				$this->Paginator->settings['order'] =  array(
						'Katilimci.' . $property => $direction
				);
			}
		}
		
		$data = $this->Paginator->paginate('Katilimci');
		$count = $this->params['paging']['Katilimci']['count'];
		//debug($data, true);
		$this->set('katilimcis', $data);
		$this->set('count', $count);
	}
	
	public function save() {
		$this->autoRender = false;
		$this->layout = 'ajax';
		if ($this->request->is('post')) {
			$this->Katilimci->create();
			$result = $this->Katilimci->save($this->request->data);
			if ($result) {
				echo "{msg:'Katılımcı Başarıyla Kaydedildi', katilimciId:" . $this->Katilimci->id .  ", success: true}";
			} else {
				echo "{msg:'hatalı', success: false}";
			}
		}
	}
	
	public function onayDegistir () {
		$this->autoRender = false;
		$this->layout = 'ajax';
		$katilimciIds = split(",", $this->request->data('katilimciIds'));
		//debug($katilimciIds, true);
		//debug($projeId, true);
		//CakeLog::write('debug',"GRUP ADI " . $grupAdi);
		$katilimci = ClassRegistry::init('Katilimci');
		
		$onay = $this->request->data('onay');
		$result = $katilimci->updateAll(
				array('onay' => $onay),
				array(
					'id' => $katilimciIds
				)
		);
		if ($result) {
			echo "{msg:'Başarıyla Tamamlandı', success: true}";
		} else {
			echo "{msg:'hatalı', success: false}";
		}
	}

}
